<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
	</head>

	<body style="padding: 0px;">
		<!--<div id="toolbar" class="receipts_toolbar_style"></div>-->
		<div>
			<form id="ff" method="post">
				<div id="p" class="easyui-panel" title="费用明细" style="width: 100%;">
					<table id="tt" style="width: 100%;">
						<!--<thead>
							<tr>
								<th field="productid" width="15%" align="center" editor="text">项目名称</th>
								<th field="listprice" width="15%" align="center" editor="{type:'datebox'}">起始时间</th>
								<th field="listprice" width="15%" align="center" editor="{type:'datebox'}">结束时间</th>
								<th field="unitcost" width="10%" align="center" editor="{type:'numberbox',options:{precision:1}}">应收金额</th>
								<th field="unitcost1" width="10%" align="center" editor="{type:'numberbox',options:{precision:1}}">实收金额</th>
								<th field="unitcost2" width="10%" align="center" editor="{type:'numberbox',options:{precision:1}}">留存金额</th>
								<th field="unitcost3" width="15%" align="center" editor="{type:'numberbox',options:{precision:1}}">缴费方式</th>
								<th field="attr1" width="13%" align="center" editor="text">备注</th>
							</tr>
						</thead>-->
					</table>
				</div>
			</form>
		</div>

		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var typeInfo = new Array(); //将typeInfo初始化为数组
		var _detailToolbarData;
		var AddDialog;
		var auth = "admin;" + hex_sha1("admin1");
		_detailToolbarData = AddDetailToolBar(23);

		var data = [{
			"icon": "icon-responsibilities",
			"func": "dutyFile()",
			"functionname": "卡证及责任书"
		}];
		$(document).ready(function(){
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			}
		});
		if(id == undefined) {
			currid = AddFlag;
		}
		///增加脚注
		getPageMaxAndMin();
		addFooter($("#new_footer"));
		///tooldata为新增的toolbar数据
		content = AddReceiptsToolBar(0, data);
		$("#toolbar").append(content); 
		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/market/tenant/getmaxminid.do";
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json", 
				success: function(result) {
					console.log(result);
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						currPage = result.ID; //表示传回的ID
						currid = result.TENANT_ID;
						$('#ff').form('load', result);
					}

				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		$("#input_id").val(id);

		function print() {
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert("错误", "当前页面为新增页面不能打印");
				return;
			} else {
				pLocation = "OriginalOrderPrint.html?id=" + currid;
				window.open(pLocation);
			}
		}

		function newdata() {
			currid = AddFlag;
			currPage = 0;
			$('#ff').form('reset');
		}

		function frontpage() {
			if(currPage >= MinPage) {
				var _url = RemoteLinkAddress + '/logistics/tenant/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() {
			if(currPage <= MaxPage) {
				var _url = RemoteLinkAddress + '/logistics/tenant/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}

		}

		function searchmore() {
			AddTabIn("收费查询", parent.$('#tabs'), "UI/Market/Receipt/receiptSearch.html");
		}

		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/market/tenant/findbyid.do?TENANT_ID=' + currid;
			getRemoteData(_url);
		}
		var projectName = [{
			"value": "1",
			"text": "租金"
		}, {
			"value": "2",
			"text": "物业费"
		}, {
			"value": "3",
			"text": "滞纳金"
		}];
		var PaymentMethod = [{
			"value": "1",
			"text": "现金"
		}, {
			"value": "2",
			"text": "预交"
		}, {
			"value": "3",
			"text": "刷卡"
		}];
		var toolbar = [{
			text: '新增',
			iconCls: 'icon-add',
			handler: function() {
				$('#tt').edatagrid('addRow');
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler: function() {
				$('#tt').edatagrid('cancelRow')
			}
		}, {
			text: '保存',
			iconCls: 'icon-save',
			handler: function() {
				$('#tt').edatagrid('saveRow')
			}
		}, {
			text: '删除',
			iconCls: 'icon-remove',
			handler: function() {
				$('#tt').edatagrid('destroyRow')
			}
		}];
		GetTable();

		function cls() {
			$('#ff').form('reset');
		}
		//		$('#ff').form('submit', {
		//			url: "",
		//			onSubmit: function() {
		//				//				var isValid = $(this).form('validate');
		//				//				if (!isValid) {
		//				//					alert("存在错误");
		//				//				}
		//				//				return isValid; 
		//			},
		//			success: function() {}
		//		});
		//		$('#input_sum').textbox({
		//			onChange: function(value) {
		//				var _trim = $.trim(value);
		//				var s = changeMoneyToChinese(_trim);
		//				$('#span_sum').html(s);
		//			}
		//		});

		function GetTable() {
			$('#tt').edatagrid({
				height: 350,
				toolbar: _detailToolbarData,
				pagination: 'true',
				rownumbers: 'true',
				singleSelect: true,
				fitColumns: true,
				autoSave: true,
				columns: [
						[{
							field: 'ID',
							title: 'ID',
							width: '0%',
							align: 'center',
						}, {
							field: 'CHARGENAME',
							title: '费用名称',
							width: '25%',
							align: 'center',
							editor: {
								type: 'textbox',
								options: {
									valueField: "CHARGENAME",
									textField: "CHARGENAME",
									editable: true,
									required: true
								}
							}
						}, {
							field: 'CHARGETYPE',
							title: '费用类型',
							width: '24%',
							align: 'center',
							editor: {
								type: 'combobox',
								options: {
									valueField: "CHARGETYPE",
									textField: "CHARGETYPE",
									editable: false,
									required: true,
									data: [{
										"CHARGETYPE": "收费",
										"CHARGETYPE": "收费"
									}, {
										"CHARGETYPE": "退费",
										"CHARGETYPE": "退费"
									}]
								}
							}
						}, {
							field: 'EVIDENCE_ID',
							title: '交易凭证号',
							width: '24%',
							align: 'center',
							editor: {
								type: 'textbox',
							}
						}, {
							field: 'SUM',
							title: '金额',
							width: '24%',
							align: 'center',
							editor: {
								type: 'numberbox',
							},
							formatter: function(value, row, index) {
								if(value) {
									return "￥" + value;
								}
							}
						}]
					]
					//				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					//					CalSum();
					//				}
			});
		}

		$("#tt").datagrid('hideColumn', 'ID');

		var dialogWidth = 400;
		var dialogHeight = 300;
		var AddDialog;

		function AddDict() {
			var pLocation = "UI/COMMON/DictData.html?id=-1";
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "添加基础信息", dialogWidth, dialogHeight);
			AddDialog.dialog('open');
		}

		function getData() {
			var boostData = JSON.parse(localStorage.getItem("longTermContractData"));
			boostData.rowsCharge = new Array();
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				var _index = i;
				boostData.rowsCharge[_index] = new Object();
				boostData.rowsCharge[_index].BOOST_ID = id;
				boostData.rowsCharge[_index].CHARGENAME = dataGridRows[i].CHARGENAME;
				boostData.rowsCharge[_index].CHARGETYPE = dataGridRows[i].CHARGETYPE;
				boostData.rowsCharge[_index].EVIDENCE_ID = dataGridRows[i].EVIDENCE_ID;
				boostData.rowsCharge[_index].SUM = dataGridRows[i].SUM;
			}
			localStorage.setItem("longTermContractData", JSON.stringify(boostData));
			//			localStorage.longTermContractData = JSON.stringify(boostData);
			console.log(boostData);
			var sendData = JSON.stringify(boostData);
			$.ajax({
				dataType: 'JSON',
				url: RemoteLinkAddress + "/market/contract/updatecharge.do",
				data: "a=" + sendData,
				type: "POST",
				success: function(result) {
					if(result.msg.search("success") > -1) {
						$.messager.alert("提示信息", "成功");
						getRefreshData();
					}
				},
				error: function(result) {
					$.messager.alert("错误信息", "失败");
				}
			});
			//			submitData(data);
		}

		function save() {
			getData();
		}

		function dutyFile() {
			AddTabIn("新建长期合同收费管理", parent.$('#tabs'), "UI/Market/BIContractCreate/BIContractAddCardAndDutyFile.html?id=-1");
			//			var pLocation = "UI/Market/BIContractCreate/BIContractAddCardAndDutyFile.html?id=-1";
			//			$("#ifrmEdit").attr("src", pLocation);
			//			constructDialog($("#dlg"), "添加卡证及责任书", 800, 380);
			//			AddDialog.dialog('open');
		}

		//		function CalSum() {
		//			var _num = 0;
		//			var dataGridRows = $("#tt").datagrid("getRows");
		//			console.log(dataGridRows);
		//			for(var i = 0; i < dataGridRows.length; i++) {
		//				dataGridRows[i].UNIT_SUM = parseInt(dataGridRows[i].UNIT_SUM);
		//				if(typeof(dataGridRows[i].CHARGETYPE) != "undefined") {
		//					_num += dataGridRows[i].UNIT_SUM;
		//				}
		//			}
		//			var num = changeMoneyToChinese(_num);
		//			$("#input_sum").val(_num);
		//			$("#span_sum").val(num);
		//		}
	</script>

</html>